package jdbc;

import java.sql.*;
import java.util.*;

public class LoadJdbcDriver {
	public static void main(String[] args) throws SQLException {

		try {
			System.out.println("Before loading oracle thin driver:");
			listDrivers();
			Driver myDriver = new oracle.jdbc.driver.OracleDriver();
			DriverManager.registerDriver(myDriver);

			// on creating instance of driver (loading),
			// its class has a static block which automatically
			// registers with the DriverManager and calling
			// DriverManager.registerDriver()
			// will result in registering driver second time,
			System.out.println("After loading oracle driver:");
			listDrivers();

		} catch (Exception e) {
			System.err.println("Exception: " + e.getMessage());
		}

		Connection connection = DriverManager.getConnection("jdbc:oracle:thin:hr/admin@localhost:1521:XE");
		System.out.println("Connection alive:" + !connection.isClosed());
	}

	private static void listDrivers() {
		Enumeration<Driver> driverList = DriverManager.getDrivers();
		while (driverList.hasMoreElements()) {
			Driver driverClass = (Driver) driverList.nextElement();
			System.out.println("   " + driverClass.getClass().getName());
		}
	}
}